package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catalog2VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author: biubiu0002
 * @Date: 2022-02-08 1:25
 **/
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        //TODO 1.查出所有的1级分类
        List<CategoryEntity> list =  categoryService.getLevel1Categories();
        model.addAttribute("categories",list);
        // 视图解析器进行拼串 classpath(resources):/templates + 返回值 + .html
        // .html
        return "index";
    }
    //index/catalog.json
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String,List<Catalog2VO>> getCatalogJSON(){
        long l1 = System.currentTimeMillis();
        Map<String,List<Catalog2VO>> map = categoryService.getCatalogJSON();
        long l = System.currentTimeMillis() - l1;
        System.out.println("消耗时间: "+l);
        return map;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}
